package com.douhl.framework.web.base;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjj on 2020/1/3.
 */
public class Constants {

    public static final String DUBBO_VERSION = "1.0.0";

    /**
     * mapper方法中查询语句开头格式，必须以select开头，才会切换数据源
     */
    public static final String MAPPER_METHOD_STARTSWITH_SELECT = "select";

    /**
     * 缓存键值
     */
    public static final Map<Class<?>, String> CACHE_KEY_MAP = new HashMap<>(5);

    /**
     * 缓存命名空间前缀
     */
    public static final String CACHE_NAMESPACE_PREFIX = "dy:custome:";

    /**
     * 多数据源枚举
     */
    public enum DataSourceEnum {
        // 主库
        MASTER("masterDataSource", true),
        // 从库
        SLAVE("slaveDataSource", false);

        /**
         * 数据源名称
         */
        private String name;
        /**
         * 是否是默认数据源
         */
        private boolean master;

        DataSourceEnum(String name, boolean master) {
            this.name = name;
            this.master = master;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isMaster() {
            return master;
        }

        public void setMaster(boolean master) {
            this.master = master;
        }

        public String getDefault() {
            String defaultDataSource = "";
            for (DataSourceEnum dataSourceEnum : DataSourceEnum.values()) {
                if (!"".equals(defaultDataSource)) {
                    break;
                }
                if (dataSourceEnum.master) {
                    defaultDataSource = dataSourceEnum.getName();
                }
            }
            return defaultDataSource;
        }

    }

    /**
     * 返回码枚举
     */
    public enum ResultCodeEnum {
        /**
         * 成功
         */
        SUCCESS(200, "成功"),
        INTERNAL_SERVER_ERROR(500, "服务器出错"),
        BAD_REQUEST(400, "请求参数出错"),
        NO_SUPPORTED_MEDIATYPE(415, "不支持的媒体类型,请使用application/json;charset=UTF-8"),
        LOGIN_FAIL(303, "登录失败"),
        LOGIN_FAIL_ACCOUNT_LOCKED(304, "用户被锁定"),
        LOGIN_FAIL_ACCOUNT_DISABLED(305, "用户未启用"),
        LOGIN_FAIL_ACCOUNT_EXPIRED(306, "用户过期"),
        LOGIN_FAIL_ACCOUNT_UNKNOWN(307, "不存在该用户"),
        LOGIN_FAIL_INCORRECT_CREDENTIALS(308, "密码不正确"),
        LOGIN_FAIL_CAPTCHA_ERROR(309, "验证码错误"),
        UNLOGIN(401, "没有登录"),
        UNAUTHORIZED(403, "没有权限"),
        SMS_SEND_ERROR(10001, "注册短信发送失败"),
        SMS_RESEND_ERROR(10002, "注册短信发送失败,重试间隔不得低于1分钟"),
        REGISTER_PHONE_EXISTED(310, "手机号已注册"),
        DATA_DUPLICATE_KEY(601, "数据重复");

        private final int value;
        private final String message;

        ResultCodeEnum(int value, String message) {
            this.value = value;
            this.message = message;
        }

        public int value() {
            return this.value;
        }

        public String getMessage() {
            return this.message;
        }

    }

    public enum CacheNamespaceEnum {
        /**
         * 数据缓存
         */
        LOCK(CACHE_NAMESPACE_PREFIX + "lock:", "分布式锁"),
        DATA(CACHE_NAMESPACE_PREFIX + "data:", "数据缓存");


        /**
         * 值
         */
        private String value;
        /**
         * 描述
         */
        private String message;

        CacheNamespaceEnum(String value, String message) {
            this.value = value;
            this.message = message;
        }

        public String value() {
            return this.value;
        }

        public String getMessage() {
            return this.message;
        }
    }

    /**
     * 缓存名
     */
    public class RcCacheName {
        /**
         * 测试
         */
        public static final String RC_CENTRAL_MACHINE = "rc_central_Machine";

        /**
         * 课程包缓存
         */
        public static final String RC_COURSE_PACKAGE = "rc_course_package";

        /**
         * 教企缓存
         */
        public static final String RC_TEACHING_ENTERPRISE = "rc_teaching_enterprise";

        /**
         * 教企课程关联表缓存
         */
        public static final String RC_TEACHING_ENTERPRISE_COURSE = "rc_teaching_enterprise_course";

        /**
         * 课程表缓存
         */
        public static final String RC_COURSE = "rc_course";


        /**
         * 基础课程包
         */
        public static final String LEVEL_BASE = "_level_base";

        /**
         * 增强课程包
         */
        public static final String LEVEL_ADD = "_level_add";

        /**
         * 首页选课中心和增强包 - 更多功能
         */
        public static final String MORE = "_More";

        /**
         * 课程包课程关联表缓存
         */
        public static final String RC_COURSE_PACKAGE_ITEM = "rc_course_package_item";

        /**
         * 课程计划开课时间表缓存
         */
        public static final String RC_COURSE_PLAN_TIME = "rc_course_plan_time";

        /**
         * 用户档案
         */
        public static final String RC_USER_PROFILE = "rc_user_profile";

        /**
         * 课程分类缓存名
         */
        public static final String COURSE_CATEGORY = "edu_course_category";
    }

    /**
     * 课程包套餐模式字典
     */
    public class CoursePackageMode {

        /**
         * 58000套餐课程包
         */
        public static final String COURSE_PACKAGE_58000 = "58000package";

        /**
         * 98000套餐课程包
         */
        public static final String COURSE_PACKAGE_98000 = "98000package";

        /**
         * 98000套餐课程包
         */
        public static final String COURSE_PACKAGE_ADD = "addpackage";
    }

    /**
     * 将数字转为周X
     *
     * @param dayOfWeek 一周中的某一天
     * @return java.lang.String
     * @author douhl
     * @date 2020/1/16 20:04
     */
    public static String toChineseWeek(Integer dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                return "周日";
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
            default:
                return null;
        }
    }

    /**
     * 个位数除0之外的补0变成两位数字符串
     * @param bitNum Integer类型的数
     * @return java.lang.String
     * @author douhl
     * @date 2020/1/16 20:24
     */
    public static String appendZero(Integer bitNum) {
        if (0 <= bitNum && bitNum < 10){
            return "0" + bitNum.toString();
        }
            return bitNum.toString();
    }
}
